package com.aoyy.util;

import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
/**==============================================================================================
 * Component class <code>Page</code>
 * @author this author
 * @date 2017-08-05
 * =============================================================================================== */
@Component
@SuppressWarnings("rawtypes")
public class Page implements Serializable {
	private static final long serialVersionUID = -119889153659550691L;
	private static final int DEFAULT_PAGE_SIZE = 10;
	private int pageSize = DEFAULT_PAGE_SIZE;
	private int recordCount;
	private int firstResult;
	private List list;
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}
	
	public int getRecordCount() {
		return recordCount;
	}
	
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}
	
	public int getFirstResult() {
		return firstResult;
	}
	
	public void setList(List list) {
		this.list = list;
	}
	
	public List getList(){
		return list;
	}
	
	public Page() {
		this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList());
	}
	
	public Page(int firstResult, int recordCount, int pageSize, List list) {
		this.firstResult = firstResult;
		this.recordCount = recordCount;
		this.pageSize = pageSize;
		this.list = list;
	}
	
	public int getPageCount() {
		return (recordCount - 1) / pageSize + 1;
	}
	
	public int getPageNo() {
	    return firstResult / pageSize + 1;	
	}
	
	public boolean isHasNext() {
		return getPageNo() < getPageCount();
	}
	
	public boolean isHasPrevious() {
		return getPageNo() > 1;
	}
	
	public static int getFirstResult(int pageNo){
		return getFirstResult(pageNo, DEFAULT_PAGE_SIZE);		
	}
	
	public static int getFirstResult(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}

}
